<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <el-col :span="12">
        <div class="opration_wrap" v-has="permissionAdd">
          <el-button type="success" icon="fa fa-plus" size="small" @click="handleClickAdd()">新增</el-button>
          <el-button type="success" icon="el-icon-refresh-right" @click="reload">刷新</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item prop="tel">
            <el-input v-model="searchForm.version" :clearable="true" placeholder="版本号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="appName" label="APP名称"></el-table-column>
        <el-table-column prop="version" label="APP版本号"></el-table-column>
        <el-table-column prop="updateVersion" label="APP版本ID"></el-table-column>
        <el-table-column prop="appUrl" label="APP下载地址"></el-table-column>
        <el-table-column prop="updateState" label="更新类型" :formatter="formatUpdateState"></el-table-column>
        <el-table-column prop="describes" label="描述"></el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" size="mini">编辑</el-button>
              <!--<el-button @click="handleClickUpdate(scope.row,'-1')" size="mini">删除</el-button>-->
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import Crumbs from '@/components/Crumbs';
import * as validators from '@/utils/validators';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import ProvinceCity from '@/components/ProvinceCity';
export default {
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      searchForm: {
        version: '',
        date: [],
      },
      permissionAdd: 'SYSTEM_MANAGER_OPERATE_VERSION_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_OPERATE_VERSION_LIST_EDIT',
      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
        currentPage: 1,
      },
      tableList: [],

    };
  },
  components: {
    Crumbs,
  },
  mounted() {
    this.init();
  },
  activated() {
    if (this.$route.params.customerId) {
      let customerItem = this.tableList.filter((item) => {
        return item.customerId == this.$route.params.customerId;
      });
      let index = this.tableList.indexOf(customerItem[0]);
      for (let item in this.tableList[index]) {
        if (this.$route.params[item]) {
          this.tableList[index][item] = this.$route.params[item];
        }
      }
    }
  },
  methods: {
    init() {
      //this.initDate();
      this.listTable();
      this.getTableHeight();
    },

    formatTime(row, column, cellValue) {
      return this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    formatUpdateState(row, column, cellValue) {
      //1.强制更新 2.选择更新 3.不用更新
      if (cellValue == 1) {
        return '强制更新';
      } else if (cellValue == 2) {
        return '选择更新';
      } else if (cellValue == 3) {
        return '不用更新';
      }
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        version: this.searchForm.version,
      };
      this.fetchData(data);
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    handleClickAdd() {
      this.$router.push({ path: '/operate/versionAdd' });
    },
    handleClickEditor(row) {
      this.$router.push({ path: '/operate/versionEdit', query: { id: row.savId } });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.listTable();
    },
    fetchData(data) {
      this.$emit('show-loading', true);
      this.$Get(this.$Config.api.versionList, data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.page.currentPage = res.data.pageNum;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
  } /*,
  beforeRouteEnter(to,from,next){
    next(vm => {
      if (from.name == '新增客户') {
      vm.init();
    }
  })
  }*/,
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
